def text1(i):
    if i==10:
        return 10
    else:
        return i+text1(i+1)
# r=text1(1)
# print(r)

def sum2(x):
    if x > 1:
        value = x + sum2(x - 1)
    else:
        value = 1
    return value
# print(sum2(15))
# 匿名函数
"""
re1 = lambda x,y:x+y
print(re1) # <function <lambda> at 0x0200E0C0>
print(re1(3,5))
a = lambda x,y:x+y() # 这种也是一样的效果
print(a) # <function <lambda> at 0x0146E150>

f = lambda x,y:x+y
print(f(1,2)) # 3
# 等同于上面
def f(x,y):
    return x+y
"""

# 匿名函数和内置函数使用
salarics={
    'xiaolong3':300000,
    'xiaolong2':3000,
    'long1':1000,
}

# def func(name):
#     return salarics[name]
# print(func("xiaolong3"))
# print(max(salarics,key=func)) # max(salarics， key = 300000,3000,1000)
"""
print(max(salarics,key = lambda name:salarics[name]))
print(min(salarics,key = lambda name:salarics[name]))
"""
# 但是我们要比较薪资 返回的要求是人名
# 薪资反序
# print(sorted(salarics,key=lambda name:salarics[name],reverse=True))

# 根据员工表中的薪资进行排序
s = [
    {'name':'龙仔','性别':'男','薪资':3000,'职位':'打工仔'},
    {'name':'曹賊','性别':'男','薪资':9000,'职位':'总经理'},
    {'name':'浩仔','性别':'男','薪资':8000,'职位':'副经理'},
    {'name':'雷子','性别':'男','薪资':5000,'职位':'督导'},
]
"""
a = sorted(s,key = lambda s:s['薪资'],reverse=True) #
print(a)
"""

# 冒泡排序
"""
for i in range(len(s)):
    for j in range(len(s)-1):
        if s[j]['薪资'] > s[j+1]['薪资']:
            s[j]['薪资'],s[j+1]['薪资'] = s[j+1]['薪资'],s[j]['薪资']
print(s)
"""

#  闭包
"""
def outer(x, y):
    def func():
        print(x + y)
    return func # 没有括号，表示没有调用，返回的就是地址值
func = outer(3,2)
print(func) # <function outer.<locals>.func at 0x01E9E108>
func()
"""
# 闭包案例，自增长
"""
def func1():
    num = 0
    def func2():
        nonlocal num
        num+=1
        return num
    return func2
f = func1()
f() # 1
f() # 2
print(f()) # 3
"""
# 装饰器
"""
def run():
    print("跑步")
    #print("健身") # 违反了装饰器原则第一
# def run():
#     print("健身")
def fitness():
    print("健身")
    run()
# fitness() #  这样就违背了装饰器的原则第二
"""
"""
name = "嘉诚"
def run(name):
    print("==========")
    print("我是%s"%name)
    print("==========")
# 装饰器就是一个特殊的闭包函数
# 1.定义了decorate 检测decorate语法 new_func没有定义
def decorate(func): # func 当成原来的函数就可以
    def new_func(new_name): #  new_name 是原来函数的形参
        print("我是被装饰器前面的代码")
        func(new_name)
        print("我是被装饰器后面的代码")
    return new_func
new_func = decorate(run)
new_func("关博文")
"""
# 装饰器案例二
"""
def eat(food):
    print(f"我吃的是{food}")


def zhuangshi(func): # func ： 就是被装饰的函数
    def luoji(food_new): # food_new：就是被装饰的函数里 形参
        print("终于到了吃饭的时间")
        func(food_new)
        print("吃的好饱啊~")
    return luoji
ss = zhuangshi(eat)
ss("脚丫子")
"""
# 装饰器案例二 ：测试for循环从1加到9000000000000的时间
"""
from datetime import datetime
# 当前时间
# print(datetime.now())
# 时间装饰器
n = 900000
def for1(n):
    sum = 0
    for i in range(1,n+1):
        # print(1)
        sum += i
    print(sum)
# for(n)
def run_time(func):# func是for1 这是一个用来计算程序执行时间的装饰器
    # print(func)
    def new_fonc(n):# for1(n) 的n
        start_time = datetime.now()
        print('开始时间%s'%start_time)
        func(n)
        end_time = datetime.now()
        print('结束时间%s'%end_time)
        time1 = end_time - start_time
        print('花费时间%s'%time1)
    return new_fonc
for1 = run_time(for1)
for1(n)
"""

# 简化装饰器

from datetime import datetime
n = 900000
def run_time(func):
    def new_func(n):
        start_time = datetime.now()
        print("开始时间%s"%start_time)
        func(n)
        """
        func(n)：
        def for1(n):
            sum = 0
            for i in range(1,n+1):
            sum+=i
        print(sum)
        """
        end_time = datetime.now()
        print("结束时间%s"%end_time)
        time1 = end_time - start_time
        print("花费时间%s"%time1)
    return new_func

@run_time
def for1(n):
    sum = 0
    for i in range(1,n+1):
        sum+=i
    print(sum)

for1(n)
"""
执行结果：
开始时间%s 
print(sum)
结束时间%s
花费时间%s
"""




